/*
 Copyright (c) 2013 MeherTJ G. All rights reserved.
 License: LGPL for personnal study or free software.
 */

#pragma once

#include "zen_app.h"

#if ZEN_OS_IOS

#include "zen_log.h"

@protocol ViewCtrl

- (void) setFPS:(int)fps;

- (int) getFPS;

- (void) setMultiTouch:(bool)enable;

- (bool) isMultiTouch;

@end

class AppIOS : public Zen::App
{
protected:
	std::shared_ptr<Zen::AppDelegate> mDelegate = nullptr;
	
	Zen::Size2 mViewSize;
	
	bool m_rotatable = true;
	
	bool m_status_visible = false;
	
	bool m_is_paused = false;

	bool m_is_multi_touch = true;

	float m_fps = 60.f;

	id <ViewCtrl> m_view_ctrl;
public:
	static AppIOS * Get();

	void setViewCtrl(id <ViewCtrl> vc) {
		m_view_ctrl = vc;
		if(m_view_ctrl) {
			[m_view_ctrl setFPS:(int)m_fps];
			[m_view_ctrl setMultiTouch: m_is_multi_touch];
		}
	}
	
	virtual void setRuntimeDelegate
	(std::shared_ptr<Zen::AppDelegate> delegate)
	override
	{
		mDelegate = delegate;
	}
	
	virtual void onUpdate()
	{
		
	}
	virtual void onDraw()
	{
		
	}
	
	virtual std::shared_ptr<Zen::AppDelegate>
	getRuntimeDelegate() override
	{
		return mDelegate;
	}
	
	virtual float getFramesPerSecond() override
	{
		if(m_view_ctrl) {
			[m_view_ctrl getFPS];
		}
		return m_fps;
	}
	virtual void setFramesPerSecond(float frames) override
	{
		m_fps = frames;
		if(m_view_ctrl) {
			[m_view_ctrl setFPS:(int)m_fps];
		}
	}
	
	virtual bool isMultiTouch() override
	{
		if(m_view_ctrl) {
			return [m_view_ctrl isMultiTouch];
		}
		return m_is_multi_touch;
	}
	virtual void setMultiTouch(bool use) override
	{
		m_is_multi_touch = use;
		if(m_view_ctrl) {
			[m_view_ctrl setMultiTouch: m_is_multi_touch];
		}
	}
	
	virtual bool isRotatable() override
	{
		return m_rotatable;
	}
	virtual void setRotatable(bool r) override
	{
		m_rotatable = r;
	}
	
	virtual bool isStatusVisible() override
	{
		return m_status_visible;
	}
	virtual void setStatusVisible(bool show) override
	{
		m_status_visible = show;
	}
	
	bool isBGUpdate() override {
		return false;
	}
	
	void setBGUpdate(bool v) override {
		if(v) Zen::LogD("BGUpdate not supported on iOS");
	}

public:
	void update();
	
	void draw();
	
	void launch(Zen::Size2 view_size);
	
	void resize(Zen::Size2 view_size);
	
	void exit();
	
	void pause();
	
	void resume();
	
	void touchDown(Zen::AppTouch);
	
	void touchUp(Zen::AppTouch);
	
	void touchCancel(Zen::AppTouch);
	
	void touchMove(Zen::AppTouch);
	
protected:
	AppIOS()
	{
		mDelegate = nullptr;
	}
};


inline void AppIOS::update()
{
	mDelegate->onUpdate();
}

inline void AppIOS::draw()
{
	mDelegate->onDraw();
}

inline void AppIOS::launch(Zen::Size2 view_size)
{
	Zen::LogD("---\napp launch ok\nscreen_size:%.2f %.2f\n"
			  "rotatable:%d\nfps:%.2f\n---",
			  view_size.w, view_size.h,
			  (int)isRotatable(),
			  (float)getFramesPerSecond()
			  );

	mDelegate->onLaunch(view_size);
	mViewSize = view_size;
}

inline void AppIOS::resize(Zen::Size2 view_size)
{
	mDelegate->onResize(view_size);
	mViewSize = view_size;
}

inline void AppIOS::exit()
{
	mDelegate->onExit();
}

inline void AppIOS::pause()
{
	if(m_is_paused) return;
	m_is_paused = true;
	mDelegate->onPause();
}

inline void AppIOS::resume()
{
	if(!m_is_paused) return;
	mDelegate->onResume();
	m_is_paused = false;
}

inline void AppIOS::touchDown(Zen::AppTouch touch)
{
	mDelegate->onTouchDown(touch);
}

inline void AppIOS::touchUp(Zen::AppTouch touch)
{
	mDelegate->onTouchUp(touch);
}

inline void AppIOS::touchCancel(Zen::AppTouch touch)
{
	mDelegate->onTouchCancel(touch);
}

inline void AppIOS::touchMove(Zen::AppTouch touch)
{
	mDelegate->onTouchMove(touch);
}

#endif
